package com.zenith.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.edu.mapper.EduAssignmentMapper;
import com.zenith.edu.po.domain.EduAssignment;
import com.zenith.edu.po.dto.EduAssignmentDTO;
import com.zenith.edu.po.vo.EduAssignmentVO;
import com.zenith.edu.service.EduAssignmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EduAssignmentServiceImpl extends ServiceImpl<EduAssignmentMapper, EduAssignment> implements EduAssignmentService {
    
    @Override
    public EduAssignmentVO createAssignment(EduAssignmentDTO dto) {
        EduAssignment assignment = new EduAssignment();
        BeanUtils.copyProperties(dto, assignment);
        this.save(assignment);
        
        EduAssignmentVO vo = new EduAssignmentVO();
        BeanUtils.copyProperties(assignment, vo);
        return vo;
    }
    
    @Override
    public EduAssignmentVO getAssignmentById(Long id) {
        EduAssignment assignment = this.getById(id);
        if (assignment == null) {
            return null;
        }
        
        EduAssignmentVO vo = new EduAssignmentVO();
        BeanUtils.copyProperties(assignment, vo);
        return vo;
    }
    
    @Override
    public EduAssignmentVO updateAssignment(EduAssignmentDTO dto) {
        EduAssignment assignment = new EduAssignment();
        BeanUtils.copyProperties(dto, assignment);
        this.updateById(assignment);
        
        EduAssignmentVO vo = new EduAssignmentVO();
        BeanUtils.copyProperties(assignment, vo);
        return vo;
    }
    
    @Override
    public boolean deleteAssignment(Long id) {
        return this.removeById(id);
    }
    
    @Override
    public List<EduAssignmentVO> getAssignmentList() {
        List<EduAssignment> assignmentList = this.list();
        return assignmentList.stream().map(assignment -> {
            EduAssignmentVO vo = new EduAssignmentVO();
            BeanUtils.copyProperties(assignment, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}